local hx__tuoyu = fk.CreateSkill {
  name = "hx__tuoyu",
  tags = { Skill.Compulsory },
}


hx__tuoyu:addEffect("active", {
  name = "hx__tuoyu",
  anim_type = "control",
  prompt = "#hx__tuoyu",
  can_use = function(self, player)
    return player:usedEffectTimes(self.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local cards = player:getCardIds("h")
    local markedcards = {{}, {}, {}}
    local card
    for _, id in ipairs(cards) do
      card = Fk:getCardById(id)
      for i = 1, 3, 1 do
        if card:getMark("@@hx__tuoyu" .. tostring(i) .. "-inhand") > 0 then
          table.insert(markedcards[i], id)
          break
        end
      end
    end
    local result = room:askToCustomDialog(player, {
      skill_name = hx__tuoyu.name,
      qml_path = "packages/huixiangkill/qml/HX__tuoyuBox.qml",
      extra_data = {
        cards,
        markedcards[1], player:getMark("hx__tuoyu1") > 0,
        markedcards[2], player:getMark("hx__tuoyu2") > 0,
        markedcards[3], player:getMark("hx__tuoyu3") > 0,
      }
    })
    if result ~= "" then
      local d = result
      for _, id in ipairs(cards) do
        card = Fk:getCardById(id)
        for i = 1, 3, 1 do
          room:setCardMark(card, "@@hx__tuoyu"..i .. "-inhand", table.contains(d[i], id) and 1 or 0)
        end
      end
    end
  end,
})

local spec = {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__tuoyu.name) and player.phase == Player.Play and
      not player:isKongcheng() and
      table.find({"1", "2", "3"}, function(n)
        return player:getMark("hx__tuoyu"..n) > 0
      end)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = player:getCardIds("h")
    local markedcards = {{}, {}, {}}
    local card
    for _, id in ipairs(cards) do
      card = Fk:getCardById(id)
      for i = 1, 3, 1 do
        if card:getMark("@@hx__tuoyu" .. tostring(i) .. "-inhand") > 0 then
          table.insert(markedcards[i], id)
          break
        end
      end
    end
    local result = room:askToCustomDialog(player, {
      skill_name = hx__tuoyu.name,
      qml_path = "packages/huixiangkill/qml/HX__tuoyuBox.qml",
      extra_data = {
        cards,
        markedcards[1], player:getMark("hx__tuoyu1") > 0,
        markedcards[2], player:getMark("hx__tuoyu2") > 0,
        markedcards[3], player:getMark("hx__tuoyu3") > 0,
      }
    })
    if result ~= "" then
      local d = result
      for _, id in ipairs(cards) do
        card = Fk:getCardById(id)
        for i = 1, 3, 1 do
          room:setCardMark(card, "@@hx__tuoyu"..i .. "-inhand", table.contains(d[i], id) and 1 or 0)
        end
      end
    end
  end,
}
hx__tuoyu:addEffect(fk.EventPhaseStart, spec)
hx__tuoyu:addEffect(fk.EventPhaseEnd, spec)

hx__tuoyu:addEffect("targetmod", {
  name = "hx__tuoyu",
  bypass_times = function(self, player, skill, scope, card)
    return card and player:hasSkill(hx__tuoyu.name) and card:getMark("@@hx__tuoyu2-inhand") > 0
  end,
  bypass_distances =  function(self, player, skill, card)
    return card and player:hasSkill(hx__tuoyu.name) and card:getMark("@@hx__tuoyu2-inhand") > 0
  end,
})

hx__tuoyu:addEffect(fk.PreCardUse, {
  can_refresh = function(self, event, target, player, data)
    return target == player and not data.card:isVirtual() and player:hasSkill(hx__tuoyu.name) and
      table.find({1, 2, 3}, function (n)
        return data.card:getMark("@@hx__tuoyu"..n.."-inhand") > 0
      end)
  end,
  on_refresh = function(self, event, target, player, data)
    if data.card:getMark("@@hx__tuoyu1-inhand") > 0 then
      if data.card.name == "analeptic" then
        if data.extra_data and data.extra_data.analepticRecover then
          data.additionalRecover = (data.additionalRecover or 0) + 1
        else
          data.extra_data = data.extra_data or {}
          data.extra_data.additionalDrank = (data.extra_data.additionalDrank or 0) + 1
        end
      else
        data.additionalDamage = (data.additionalDamage or 0) + 1
        data.additionalRecover = (data.additionalRecover or 0) + 1
      end
    elseif data.card:getMark("@@hx__tuoyu2-inhand") > 0 then
      data.extraUse = true
    elseif data.card:getMark("@@hx__tuoyu3-inhand") > 0 then
      data.disresponsiveList = table.simpleClone(player.room.alive_players)
    end
  end,
})

return hx__tuoyu
